#include "keydispatcher.h"
#include <stdio.h>
#include <pthread.h>

#define KEY 0x01
#define REL 0x02
#define ABS 0x03

class Log
{
public:
    void log(int type, int code, int value) {
        do {
            if (type == KEY) {
                printf("KEY\n");
                printf("---------------------------------------------------\n");
                printf("type:%d, code:%d, value:%d \n", type, code, value);
                printf("+++++++++++++++++++++++++++++++++++++++\n");
                break;
            } 
            if (type == REL) {
                printf("REL\n");
                printf("---------------------------------------------------\n");
                printf("type:%d, code:%d, value:%d \n", type, code, value);
                printf("+++++++++++++++++++++++++++++++++++++++\n");
                break;
            }
            if (type == ABS) {
                printf("ABS\n");
                printf("---------------------------------------------------\n");
                printf("type:%d, code:%d, value:%d \n", type, code, value);
                printf("+++++++++++++++++++++++++++++++++++++++\n");
                break;
            }
        } while(0);
        fflush(stdout);
    }
};

void* pt_callback(void * arg)
{
    KeyDispatcher *keydispatcher = (KeyDispatcher*)(arg);
    keydispatcher->run();
    return NULL;
}

int main()
{
    pthread_t ptid;
    KeyDispatcher dispatcher;
    Log logger;
    dispatcher.m_key.connect(&logger, &Log::log);
    if (dispatcher.init()) {
        int ptre = pthread_create(&ptid, NULL, pt_callback, (void*)(&dispatcher));
        if (ptre != 0) {
            printf("dispatcher thread create fail!\n");
        } 
        else {
            pthread_join(ptid, NULL);
        }
        //dispatcher.run();
    } 
    else {
        printf("init fail!\n");
    }

    return 0;
}